package com.feeyo.groundservice.zxing.activity

import com.feeyo.groundservice.base_util.post
import com.feeyo.groundservice.zxing.BaseZxingActivity
import com.feeyo.groundservice.zxing.constant.ZxingConstant
import com.feeyo.groundservice.zxing.event.ScanCodeResultEvent
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeResult
import com.journeyapps.barcodescanner.DefaultDecoderFactory
import com.therouter.router.Route

/**
 * This sample performs continuous scanning, displaying the barcode and source image whenever
 * a barcode is scanned.
 */
@Route(path = ZxingConstant.ContinuousCaptureActivity)
internal class ContinuousCaptureActivity : BaseZxingActivity() {

    private val formats = listOf(
        BarcodeFormat.QR_CODE,
        BarcodeFormat.CODE_39,
        BarcodeFormat.CODE_93,
        BarcodeFormat.CODE_128,
        BarcodeFormat.PDF_417
    )
    override fun differentSettings() {
        binding.barcodeScanner.apply {
            barcodeView.decoderFactory = DefaultDecoderFactory(formats)
            decodeContinuous(callback)
        }
    }

    override fun validateCodeFormat(result: BarcodeResult): Boolean {
        return true
    }

    override fun dataCallback(format: String) {
        scanCallback?.onScanCodeSuccess(lastText,format) ?: ScanCodeResultEvent(lastText,format).post()
    }
}
